package com.wise.service.cms.content.resourcegroup;

import com.wise.entities.content.ResourceGroup;
import com.wise.entities.content.ResourceStatus;
import com.wise.framework.basic.DateTimeUtil;
import com.wise.framework.daomodel.DaoOne;
import com.wise.framework.daomodel.DaoSave;
import com.wise.framework.entitiesbasic.IdTimeEntity;
import com.wise.framework.logger.Logger;
import com.wise.framework.session.CurrentUserId;
import com.wise.framework.session.GetAcceptLanguage;
import com.wise.framework.validation.Precondition;
import com.wise.summer.v1.cms.content.resourcegroup.AddResourceGroup;
import org.springframework.stereotype.Service;
import org.summerframework.model.SummerService;
import org.summerframework.model.SummerServiceBean;

import java.sql.Timestamp;
import java.util.Objects;

import static com.wise.model.content.ContentErrMsgs.Err_100100000008;


/**
 * @author yubo
 */
@Service
@SummerService
public class AddResourceGroupsService implements SummerServiceBean<AddResourceGroup> {
	private static final Logger log = Logger.getLogger(AddResourceGroupsService.class);

	@Override
	public void sum(AddResourceGroup summer) {
		String lang = GetAcceptLanguage.s(true);
		ResourceGroup groups = DaoOne.s(ResourceGroup.class, summer.getGroupName(), null);
		//判断是否查询到数据
		Precondition.checkState(Objects.isNull(groups), Err_100100000008);

		groups = new ResourceGroup();
		groups.setCount(0);
		groups.setGroupName(summer.getGroupName());
		groups.setGroupType(summer.getGroupType());
		groups.setStatus(ResourceStatus.Normal.value());
		groups.setLang(lang);
		groups.setCenter(summer.getCenter());

		setCreateBy(groups);
		DaoSave.s(groups);

		summer.setSummerResult(true);
	}

	private void setCreateBy(IdTimeEntity idTimeEntity) {
		String userId = CurrentUserId.s(true);
		idTimeEntity.setCreatedBy(userId);
		idTimeEntity.setLastUpdateBy(userId);
		Timestamp timestamp = DateTimeUtil.timestampNow();
		idTimeEntity.setCreationDate(timestamp);
		idTimeEntity.setLastUpdateDate(timestamp);
	}

}
